﻿//map
var map;
var mapOptions;
//var mapCentered = false; //because the map is hidden (in a tab that is not shown be default), the map must be recentered when shown the first time

//lazy load google maps
function loadMaps() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis.com/maps/api/js?sensor=false&' +
        'callback=initializeMap';
    document.body.appendChild(script);
}

//callback for google map lazy load
function initializeMap() {
    mapOptions = {
        zoom: 14,
        center: new google.maps.LatLng(43.771601, -79.621527),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); //load map

    //create markers for top 5 locations and add to map

    var geocoder = new google.maps.Geocoder();
    var address = $("#map_canvas").data("address");

    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: name,
                url: "https://maps.google.com/maps?saddr=7600 Highway 27, Vaughan, ON L4H0P8 &daddr=" + address
            });

            google.maps.event.addListener(marker, 'click', function () {
                window.location.href = marker.url; //TODO: change click event to popup dialog
            });

        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });

}


$(document).ready(function () {

    //set location's website field value
    var website = $('#siteURL').data("website");
    if (website === null || website === undefined || website === "") {
        $('#siteURL').html("No Website available");
    } else {
        $('#siteURL').html("<a href='" + website + "'>" + website + "</a>");
    }

    //contactEmail
    var contactEmail = $('#contactEmail').data("email");
    if (contactEmail === null || contactEmail === undefined || contactEmail === "") {
        $('#contactEmail').html("No Email available");
    } else {
        $('#contactEmail').html("<a href='mailto:" + contactEmail + "'>" + contactEmail + "</a>");
    }

    //contactPhone
    var contactPhone = $('#contactPhone').data("phone");
    if (contactPhone === null || contactPhone === undefined || contactPhone === "") {
        $('#contactPhone').html("No Phone Number available");
    } else {
        $('#contactPhone').html("<a href='tel:" + contactPhone + "'>" + contactPhone + "</a>");
    }

    loadMaps();
    initializeMap();
});
